﻿namespace TradePublish.Service.impl
{
    using log4net;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using TradePublish.Entity;

    internal class SearchEmailManager
    {
        private BaiduManager baiduManager = new BaiduManager();
        private BingManager bingManager = new BingManager();
        private ILog log = null;
        private SogouManager sogouManager = new SogouManager();

        public SearchEmailManager()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public List<ApplyUserContract> grasp(string applyUserName)
        {
            List<ApplyUserContract> list = new List<ApplyUserContract>();
            try
            {
                int num;
                ApplyUserContract contract;
                Hashtable hashtable = new Hashtable();
                this.log.Debug("开始采集搜狗：keyword=" + applyUserName);
                List<string> list2 = this.sogouManager.graspUrl(applyUserName);
                if (list2 != null)
                {
                    for (num = 0; num < list2.Count; num++)
                    {
                        if (!(hashtable.ContainsKey(list2[num]) || (list2[num].IndexOf("360buy") >= 0)))
                        {
                            hashtable.Add(list2[num], list2[num]);
                        }
                    }
                }
                this.log.Debug("开始采集Bing：keyword=" + applyUserName);
                List<string> list3 = this.bingManager.graspUrl(applyUserName);
                if (list3 != null)
                {
                    for (num = 0; num < list3.Count; num++)
                    {
                        if (!(hashtable.ContainsKey(list3[num]) || (list3[num].IndexOf("360buy") >= 0)))
                        {
                            hashtable.Add(list3[num], list3[num]);
                        }
                    }
                }
                List<string> list4 = null;
                bool flag = false;
                if ((hashtable == null) || (hashtable.Count == 0))
                {
                    this.log.Debug("开始采集baidu：keyword=" + applyUserName);
                    flag = true;
                    list4 = this.baiduManager.graspUrl(applyUserName);
                    if (list4 != null)
                    {
                        for (num = 0; num < list4.Count; num++)
                        {
                            if (!(hashtable.ContainsKey(list4[num]) || (list4[num].IndexOf("360buy") >= 0)))
                            {
                                hashtable.Add(list4[num], list4[num]);
                            }
                        }
                    }
                }
                List<string> list5 = new List<string>();
                this.log.Debug("开始采集详细页：keyword=" + applyUserName);
                list5.Add("┌─────────────────────────────────────────");
                list5.Add("│" + applyUserName + "\t\t" + (((list4 == null) || (list4.Count == 0)) ? ("百度" + (flag ? "\x00d7" : "(未)")) : ("百度" + list4.Count)) + "\t" + (((list2 == null) || (list2.Count == 0)) ? "搜狗\x00d7" : ("搜狗" + list2.Count)) + "\t" + (((list3 == null) || (list3.Count == 0)) ? "Bing\x00d7" : ("Bing" + list3.Count)));
                if (hashtable.Count == 0)
                {
                    contract = new ApplyUserContract {
                        email = "",
                        emailSourceHtml = "【标记！无官网】",
                        applyUserName = applyUserName
                    };
                    list.Add(contract);
                    list5.Add("│无官网。");
                }
                else
                {
                    foreach (DictionaryEntry entry in hashtable)
                    {
                        string url = entry.Key.ToString();
                        this.log.Debug("开始进入官网首页采集：keyword=" + applyUserName+" url="+url);
                        List<ApplyUserContract> list6 = new GraspIntelEmailManager(url, applyUserName).execute();
                        this.log.Debug("结束官网首页采集：keyword=" + applyUserName);
                        if ((list6 == null) || (list6.Count == 0))
                        {
                            contract = new ApplyUserContract {
                                email = "",
                                emailSourceHtml = "【标记！有官网无数据】<a href='" + url + "' target='_blank'>官网" + url + "</a>",
                                applyUserName = applyUserName
                            };
                            contract.haveOfficeWebsite = 1;
                            contract.officalWebsite = url; 
                            list.Add(contract);
                            list5.Add("│-----有官网未采集到任何邮箱    \t" + url);
                        }
                        else
                        {
                            foreach (ApplyUserContract contract2 in list6)
                            {
                                list5.Add("│" + (((contract2.email == null) || contract2.email.Equals("")) ? "【标记！flash】" : contract2.email) + "   \t" + contract2.emailSourceHtml);
                                contract2.haveOfficeWebsite = 1;
                                contract2.officalWebsite = url; 
                                list.Add(contract2);
                            }
                        }
                        list6 = null;
                    }
                }
                list5.Add("└──────────────────────────────────────────");
                foreach (string str2 in list5)
                {
                    this.log.Info(str2);
                }
            }
            catch (Exception exception)
            {
                this.log.Error("%%%%%%%%莫名异常", exception);
            }
            return list;
        }
    }
}

